package com.robot.coupon.controller;

import com.alibaba.fastjson.JSON;
import com.robot.coupon.pojo.Coupon;
import com.robot.coupon.service.CouponService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * coupon控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/coupon")
public class CouponJsonController {
    @Autowired
    private CouponService couponService;

    /**
     * 传入对象来增加coupon
     *
     * @param coupon
     */
    @RequestMapping(value = "third/addCouponByCoupon", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public void thirdAddCouponByCoupon(@RequestBody String coupon) {
        couponService.add(JSON.parseObject(coupon, Coupon.class));
    }

    /**
     * 传入对象来修改coupon
     *
     * @param coupon
     */
    @PutMapping(value = "third/updateCouponByCoupon", produces = "application/json; charset=utf-8")
    @Transactional(rollbackFor = Exception.class)
    public void thirdUpdateCouponByCoupon(@RequestBody String coupon) {
        couponService.update(JSON.parseObject(coupon, Coupon.class));
    }

    /**
     * 分页+多条件查询coupon
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "third/findCouponsBySearch/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindCouponsBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Coupon> pageList = couponService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据ID查找coupon
     *
     * @param id
     * @return
     */
    @GetMapping(value = "third/findCouponById/{id}", produces = "application/json; charset=utf-8")
    public String thirdFindCouponById(@PathVariable String id) {
        return JSON.toJSONString(couponService.findById(id));
    }

    /**
     * 根据会员ID查找该会员名下特定状态的所有优惠券并分页
     *
     * @param memberId
     * @param status
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "third/findCouponsByMemberIdAndStatus/{memberId}/{status}/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindCouponsByMemberIdAndStatus(@PathVariable String memberId, @PathVariable String status, @PathVariable int page, @PathVariable int size) {
        Page<Coupon> pageData = couponService.findAllByMemberIdAndStatus(memberId, status, page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 根据会员ID查找该会员名下的某张优惠券
     *
     * @param id
     * @param memberId
     * @return
     */
    @GetMapping(value = "third/findCouponByIdAndMemberId/{id}/{memberId}", produces = "application/json; charset=utf-8")
    public String thirdFindCouponByIdAndMemberId(@PathVariable String id, @PathVariable String memberId) {
        return JSON.toJSONString(couponService.findByIdAndMemberId(id, memberId));
    }
}
